package com.qtenv.datasync.service.controller;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.json.JSONUtil;
import com.qtenv.datasync.base.Result;
import com.qtenv.datasync.service.entity.BiZbx;
import com.qtenv.datasync.service.service.BiZbxService;
import com.qtenv.datasync.utils.CustomUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * BI指标值 前端控制器
 * </p>
 *
 * @author CBL
 * @since 2022-08-01
 */
@RestController
@RequestMapping("/bi/zbx")
public class BiZbxController {

    @Autowired
    private BiZbxService biZbxService;

    @GetMapping("/getBiOverviewsData")
    public Result<?> getBiOverviewsData(String tenantId) {
        Map<String, Object> overviewsMap = MapUtil.newHashMap();

        overviewsMap.put("month", this.transformOverviewData(biZbxService.getNewestMonthOverview(tenantId)));
        overviewsMap.put("year", this.transformOverviewData(biZbxService.getNewestYearOverview(tenantId)));
        String[] keyArr = {"flx", "zbz", "zbz1", "zbz2", "zbz3"};
        overviewsMap.put("prodSaleList", this.transformProdSaleList(biZbxService.getYearProdSaleList(tenantId), keyArr));

        return Result.ok(overviewsMap);
    }

    @GetMapping("/getZoneProdSaleList")
    public Result<?> getZoneProdSaleList(@RequestParam("tenantId") String tenantId) {
        return Result.ok(biZbxService.getZoneProdSaleList(tenantId));
    }

    @GetMapping("/getDmaProdSaleList")
    public Result<?> getDmaProdSaleList(@RequestParam("tenantId") String tenantId, @RequestParam(value = "areaId", required = false) String areaId) {
        return Result.ok(biZbxService.getDmaProdSaleList(tenantId, areaId == null ? "" : areaId));
    }

    @GetMapping("/getZoneProdSaleRatio")
    public Result<?> getZoneProdSaleRatio(@RequestParam("tenantId") String tenantId, @RequestParam("areaId") String areaId) {
        return Result.ok(biZbxService.getZoneProdSaleRatio(tenantId, areaId));
    }

    @GetMapping("/getCompanyAssetsData")
    public Result<?> getCompanyAssetsData(@RequestParam("tenantId") String tenantId) {
        return Result.ok(this.transformAssetsDataList(biZbxService.getCompanyAssetsData(tenantId)));
    }

    @GetMapping("/getZoneTagInfo")
    public Result<?> getZoneTagInfo(@RequestParam("tenantId") String tenantId, @RequestParam("areaId") String areaId) {
        String[] keyArr = {"分区名称", "建设状态", "负责人", "DMA数量（个）", "考核表数量（个）", "大用户数量（个）", "管网长度（KM）"};
        String[] valueArr = {"flx", "zbz1", "zbz2", "zbz3", "zbz4", "zbz5", "zbz6"};
        return Result.ok(CustomUtil.transformZbxToTextValueList(biZbxService.getZoneTagInfo(tenantId, areaId), keyArr, valueArr));
    }

    @GetMapping("/getDmaTagInfo")
    public Result<?> getDmaTagInfo(@RequestParam("tenantId") String tenantId, @RequestParam("areaId") String areaId) {
        String[] keyArr = {"分区名称", "建设状态", "负责人", "考核表数量（个）", "大用户数量（个）", "居民用户数量（户）", "管网长度（KM）"};
        String[] valueArr = {"flx", "zbz1", "zbz2", "zbz4", "zbz5", "zbz3", "zbz6"};
        return Result.ok(CustomUtil.transformZbxToTextValueList(biZbxService.getDmaTagInfo(tenantId, areaId), keyArr, valueArr));
    }

    @GetMapping("/getZoneLatestProdSaleList")
    public Result<?> getZoneLatestProdSaleList(@RequestParam("tenantId") String tenantId, @RequestParam("areaId") String areaId) {
        String[] keyArr = {"flx", "zbz1", "zbz2", "zbz3", "zbz4"};
        return Result.ok(this.transformProdSaleList(biZbxService.getZoneLatestProdSaleList(tenantId, areaId), keyArr));
    }

    @GetMapping("/getDmaLatestProdSaleList")
    public Result<?> getDmaLatestProdSaleList(@RequestParam("tenantId") String tenantId, @RequestParam("areaId") String areaId) {
        String[] keyArr = {"flx", "zbz1", "zbz2", "zbz3", "zbz4"};
        return Result.ok(this.transformProdSaleList(biZbxService.getDmaLatestProdSaleList(tenantId, areaId), keyArr));
    }

    private List<Map<String, Object>> transformOverviewData(BiZbx biZbx) {
        List<Map<String, Object>> result = CollectionUtil.newArrayList();

//        if (biZbx != null) {
        System.out.println(JSONUtil.toJsonPrettyStr(biZbx));
        String[] keyMap = {"供水量（m³）", "售水量（m³）", "无收益水量（m³）", "产销差率（%）"};
        String[] valueMap = {"zbz", "zbz1", "zbz2", "zbz3"};
        String[] momMap = {"zbz4", "zbz6", "zbz8", "zbz10"};
        String[] yoyMap = {"zbz5", "zbz7", "zbz9", "zbz11"};

        for (int i = 0; i < 4; i++) {
            Map<String, Object> tmpMap = MapUtil.newHashMap();

            tmpMap.put("text", keyMap[i]);
            tmpMap.put("value", biZbx != null ? BeanUtil.getFieldValue(biZbx, valueMap[i]) : 0);
            tmpMap.put("mom", biZbx != null ? BeanUtil.getFieldValue(biZbx, momMap[i]) : 0);
            tmpMap.put("yoy", biZbx != null ? BeanUtil.getFieldValue(biZbx, yoyMap[i]) : 0);

            result.add(tmpMap);
        }
//        }

        return result;
    }

    private Map<String, Object> transformProdSaleList(List<BiZbx> biZbxList, String[] keyArr) {
        Map<String, Object> result = MapUtil.newHashMap();

        if (CollectionUtil.isNotEmpty(biZbxList)) {
            System.out.println(JSONUtil.toJsonPrettyStr(biZbxList));
            result.put("xAxis", biZbxList.stream().map(item -> BeanUtil.getFieldValue(item, keyArr[0])).collect(Collectors.toList()));
            result.put("supply", biZbxList.stream().map(item -> BeanUtil.getFieldValue(item, keyArr[1])).collect(Collectors.toList()));
            result.put("sale", biZbxList.stream().map(item -> BeanUtil.getFieldValue(item, keyArr[2])).collect(Collectors.toList()));
            result.put("free", biZbxList.stream().map(item -> BeanUtil.getFieldValue(item, keyArr[3])).collect(Collectors.toList()));
            result.put("prodSale", biZbxList.stream().map(item -> {
                        Object value = BeanUtil.getFieldValue(item, keyArr[4]);
                        return value == null ? 0 : value.toString().replace("%", "");
                    }).collect(Collectors.toList())
            );
        }

        return result;
    }

    private List<Map<String, Object>> transformAssetsDataList(BiZbx biZbx) {
        String[] keyArr = {"大分区数量（个）", "DMA数量（个）", "大用户数量（个）", "监测点数量（个）", "户表数量（个）"};
        String[] valueArr = {"zbz1", "zbz2", "zbz3", "zbz4", "zbz5"};

        return CustomUtil.transformZbxToTextValueList(biZbx, keyArr, valueArr);
    }

//    private List<Map<String, Object>> transformTagInfoList(BiZbx biZbx, String[] keyArr, String[] valueArr) {
//        return this.transformZbxToKeyValueList(biZbx, keyArr, valueArr);
//    }

//    private List<Map<String, Object>> transformZbxToKeyValueList(BiZbx biZbx, String[] keyArr, String[] valueArr) {
//        List<Map<String, Object>> result = CollectionUtil.newArrayList();
//
//        if (biZbx != null) {
//            for (int i = 0; i < keyArr.length; i++) {
//                Map<String, Object> tmpMap = CollectionUtil.newHashMap();
//
//                tmpMap.put("text", keyArr[i]);
//                tmpMap.put("value", BeanUtil.getFieldValue(biZbx, valueArr[i]));
//
//                result.add(tmpMap);
//            }
//        }
//
//        return result;
//    }
}
